<?PHP
	require 'includes/master.inc.php';
	
	if(isset($_GET['app_id']))
	{
		$app = new App($_GET['app_id']);
		if($app->id == "") redirect(WEB_ROOT . 'orders.php');
		$where = "app_id = '{$app->id}' AND ";
		$rss_params = "&amp;id=" . $app->id;
	}
	else
	{
		$where = '';
		$rss_params = '';
	}

	$where = isset($app) ? "app_id = '{$app->id}' AND " : '';
	
	$o = new Order();
	$orders = $o->glob("WHERE $where deleted = 0 ORDER BY id DESC");
	
	$a = new App();
	$apps = $a->glob("ORDER BY name");

	if(isset($_GET['act']) && $_GET['act'] == "rss")
	{
		$feed = new RSS();
		
		if(isset($app))
		{
			$feed->title       = "{$app->name} Orders";
			$feed->link        = str_replace("act=rss", "", full_url());
			$feed->description = $feed->title;
		}
		else
		{
			$feed->title       = "All Orders";
			$feed->link        = str_replace("act=rss", "", full_url());
			$feed->description = $feed->title;
		}

		foreach($orders as $o)
		{
			$html = "<table>";
			foreach($o->columns as $k => $v)
			{
				if($k != "license")
					$html .= "<tr><th style='text-align:right;'>$k</th><td>$v</td></tr>";
			}
			$html .= "</table>";
			
			$item = new RSSItem();
			$item->title = '[' . $apps[$o->app_id]->name . '] ' . $o->payer_email;
			$item->link  = 'http://' . $_SERVER['SERVER_NAME'] . WEB_ROOT . 'orders.php#' . $o->id;
			$item->setPubDate($o->dt); 
			$item->description = $html;
			$feed->addItem($item);
		}
		$feed->serve();
		exit;
	}

	$auth->admin();	
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
	<title>View Orders | Appcaster</title>
	<?PHP include 'inc/meta.inc.php'; ?>
	<link rel="alternate" type="application/rss+xml" title="VHX Orders" href="<?PHP WEBROOT();?>orders.php?act=rss<?PHP echo $rss_params;?>" />
</head>

<body id="home">
	<?PHP include 'inc/head.inc.php'; ?>
	<div id="nav">
		<ul>
			<li><a href='<?PHP WEBROOT();?>index.php'>Applications</a></li>
			<li class='selected'><a href='<?PHP WEBROOT();?>orders.php'>Orders</a></li>
			<li><a href='<?PHP WEBROOT();?>feedback.php'>Feedback (<?PHP echo Feedback::newCountTotal();?>)</a></li>
			<li><a href='<?PHP WEBROOT();?>stats.php'>Stats</a></li>
			<li><a href='<?PHP WEBROOT();?>options.php'>Options</a></li>
		</ul>
	</div>

	<div id="menutabs">
		<ul id="tabnav">
			<li class="<?PHP if(!isset($_GET['app_id'])) echo 'selected'; ?>"><a href='<?PHP WEBROOT();?>orders.php'>All Orders</a></li>
			<?PHP foreach($apps as $a) : ?>
			<li class="<?PHP if($_GET['app_id'] == $a->id) echo 'selected'; ?>"><a href="<?PHP WEBROOT();?>orders.php?app_id=<?PHP echo $a->id;?>"><?PHP echo $a->__name; ?></a></li>
			<?PHP endforeach; ?>
			<li><a href='<?PHP WEBROOT();?>manualorder.php<?PHP if(isset($app)) echo '?app_id=' . $app->id;?>'>Manual Order</a></li>
		</ul>
	</div>

	<div id="contain">
		<p>Total Orders: <?PHP echo count($orders); ?></p>
		<table class="grid">
			<tr>
				<th>ID</th>
				<?PHP if(!isset($app)) echo "<th>Application</th>"; ?>
				<th>Name</th>
				<th>Email</th>
				<th>Type</th>
				<th>Order Date</th>
				<th>Amount</th>
				<th>License</th>
			</tr>
			<?PHP foreach($orders as $o) : ?>
			<tr>
				<td><?PHP echo $o->id; ?></td>
				<?PHP if(!isset($app)) echo "<td>" . $apps[$o->app_id]->__name . "</td>"; ?>
				<td><a href='<?PHP WEBROOT();?>order.php?id=<?PHP echo $o->id;?>'><?PHP echo $o->first_name . " " . $o->last_name; ?></a></td>
				<td><a href='<?PHP WEBROOT();?>mailto:echo<?PHP echo $o->payer_email;?>'><?PHP echo $o->payer_email; ?></a></td>
				<td><?PHP echo $o->type; ?></td>
				<td><?PHP echo dater("m/d/y g:ia", $o->dt); ?></td>
				<td>$<?PHP echo number_format($o->payment_gross, 2); ?></td>
				<td><a href='<?PHP WEBROOT();?>license.php?id=<?PHP echo $o->id;?>'>Download</a></td>
			</tr>			
			<?PHP endforeach; ?>
		</table>
	</div>
</body>
</html>